package com.ext.aic.apis.other;

import com.ext.aic.AICApplication;
import com.ext.aic.beans.response.AppUpdateBody;
import com.ext.aic.beans.response.AppUpdateResponse;
import com.ext.aic.utils.ToastyUtils;
import com.ext.aic.utils.http.HttpUtils;
import com.eyinfo.android_pure_utils.utils.JsonUtils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;

public class AppUpdateRequest {

    protected void onAppUpdateSuccess(AppUpdateBody response) {

    }

    protected void onAppUpdateEnd() {

    }

    public void request() {

        String url = "http://geease.cn:11000/mock/11/version/update";
        Request request = new Request.Builder().url(url).get().build();
        OkHttpClient client = HttpUtils.getInstance().getClient(AICApplication.getInstance().getApplicationContext());
        client.newCall(request).enqueue(new okhttp3.Callback() {
            @Override
            public void onResponse(Call call, okhttp3.Response response) throws IOException {
                if (response.isSuccessful()) {
                    String responseBody = response.body().string();
                    AppUpdateResponse appUpdateResponse = JsonUtils.parseT(responseBody, AppUpdateResponse.class);
                    if (appUpdateResponse.getCode() == 200) {
                        onAppUpdateSuccess(appUpdateResponse.getData());
                    }
                }
                onAppUpdateEnd();
            }

            @Override
            public void onFailure(Call call, IOException e) {
                onAppUpdateEnd();
            }
        });
    }
}
